﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
////
using GalaSoft.MvvmLight.Messaging;
using ScheduleMVVM.Model.RuntimeFramework;

namespace ScheduleMVVM.Model.RuntimeFramework
{
    /// <summary>
    /// DepartsInString field updater
    /// </summary>
    internal class UpdateClock
    {
        private Thread updateThread;
        private AutoResetEvent trigger;
        public UpdateClock()
        {
            trigger = new AutoResetEvent(false);
            updateThread = new Thread(new ThreadStart(UpdaterRoutine));
            updateThread.IsBackground = true;
            updateThread.Start();
        }
        /// <summary>
        /// Forces field update
        /// </summary>
        public void ForceTick()
        {
            trigger.Set();
        }
        private void UpdaterRoutine()
        {
            while (true)
            {
                trigger.WaitOne(60 * 1000); // 1 minute
                Messenger.Default.Send<UpdateTick>(new UpdateTick());
            }
        }
    }
}
